package com.zkyc.framework.common.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkyc.framework.common.pojo.Role;
import com.zkyc.framework.common.pojo.Website;
import com.zkyc.framework.common.vo.role.RoleVo;
import org.apache.ibatis.annotations.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * (Role)表数据库访问层
 *
 * @author wj
 * @since 2022-05-25 16:19:49
 */
@Mapper
public interface RoleMapper extends BaseMapper<Role> {
    @Select({
            "<script>",
            " SELECT  a.role_id  " +
            " from  system_website_ref_role a" +
            " where  a.website_id=#{websiteId} ",
            "</script>"
    })
    List<Integer> getRoleIdListByWebsiteId(@Param("websiteId") Long websiteId);

    @Select({
            "<script>",
            " select  a.id,a.role_name  from  system_role a" +
            " where  a.id in (select  b.role_id   " +
            " from  system_website_ref_role b " +
            " where b.id=#{websiteId}  ",
            "</script>"
    })
    @Result(id = true, property = "roleId", column = "id")
    List<RoleVo> getRoleList(@Param("websiteId") Long websiteId);


    @Select({
            "<script>",
            " SELECT f.* from system_website_ref_role e ,system_role f where  e.website_id in(" +
            " SELECT c.website_id from system_user_ref_role a,system_role b,system_website_ref_role c " +
            " where a.user_id=#{userId}" +
            " and a.role_id=b.id",
            " and c.role_id=b.id)",
            " and e.role_id=f.id" +
            " and f.role_type=1",
            "</script>"
    })
    List<Role> queryAllRoleListByUserId(@Param("userId") String userId);


    @Select({
            "<script>",
            "  SELECT *  from system_role " +
            "  where id in(SELECT a.role_id from system_user_ref_role a, system_website_ref_role b " +
            "  where a.user_id=#{userId}",
            "  and b.role_id=a.role_id",
            "  and b.website_id=#{websiteId})",
            "</script>"
    })
    List<Role> queryRoleByUserIdWebsiteId(@Param("userId") String userId, @Param("websiteId") Integer websiteId);

    //角色列表
    @Select({
            "<script>",
            " SELECT  f.* FROM " +
            " system_website_ref_role e, " +
            " system_role f  " +
            " WHERE " +
            " e.website_id IN (SELECT " +
            " c.website_id  " +
            " FROM " +
            " system_user_ref_role a, " +
            " system_role b, " +
            " system_website_ref_role c  " +
            " WHERE  a.user_id =#{userId} " +
            " AND a.role_id = b.id  " +
            " AND c.role_id = b.id )  " +
            " AND e.role_id = f.id " +
            " AND f.is_deleted=0" +
            " ORDER BY f.gmt_updated DESC",
            "</script>"
    })
    Page<Role> queryRoleListByUserId(@Param("userId") String userId, Page rolePage);

    @Select({
            "<script>",
            " SELECT DISTINCT   d.*   " +
            " FROM   " +
            "   system_user_ref_role a,   " +
            "   system_role b,   " +
            "   system_website_ref_role c ,   " +
            "   web_website d   " +
            " WHERE   " +
            "   a.user_id =#{userId}" +
            " AND a.role_id = b.id    " +
            " AND c.role_id = b.id    " +
            " AND d.id=c.website_id ",
            "</script>"
    })
    List<Website> queryWebsiteListByUserId(@Param("userId") String userId);
}

